﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;

namespace LDSP.UI
{
    public partial class PasswordChange : LDSP.BRL.BasePage
    {
        public override void RedirectBefore()
        {
            if (LoginAdmin == null && LoginUser == null)
            {
                this.GoToLogin();
            }
        }

        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                string password = this.LoginAdmin != null ? this.LoginAdmin.Password : this.LoginUser.Password;
                string p = this.txt_old.Text;

                if (!password.Equals(p))
                {
                    this.JSAlert("旧密码输入错误，请重试！");
                    return;
                }
                else if (!string.IsNullOrEmpty(this.txt_new.Text) && this.txt_new.Text != this.txt_new1.Text)
                {
                    this.JSAlert("两次输入的新密码不匹配，请重试！");
                    return;
                }
                else
                {
                    UpdatePassword(this.txt_new.Text);

                    this.JSAlert("密码更新成功！");
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        private void UpdatePassword(string p)
        {
            if (this.LoginAdmin != null)
            {
                if (this.LoginAdmin.IsSuper)
                {
                    SystemConfig.SuperUserPwd = p;
                    this.LoginAdmin.Password = p;
                }
                else
                {
                    this.LoginAdmin.Password = p;
                    this.LoginAdmin.Update();
                }
            }
            else if (this.LoginUser != null)
            {
                this.LoginUser.Password = p;
                this.LoginUser.Update();
            }
            else
            {
                throw new Exception("用户未登录。");
            }
        }
    }

}